package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.IndexCategory;
import com.sunday.pinxinghui.entity.Product;
import com.sunday.pinxinghui.model.products.ProductDetailActivity;


import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/27.
 */

public class ProductListAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<Product> dataSet;
    private LayoutInflater inflater;
    public ProductListAdapter(Context context, List<Product> datas) {
        this.mContext = context;
        this.dataSet = datas;

        inflater = LayoutInflater.from(mContext);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.layout_product_list_item, null);
        return new ProductHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ProductHolder productHolder = (ProductHolder) holder;
        Product item = dataSet.get(position);
        if (!TextUtils.isEmpty(item.getImage())) {
            Glide.with(mContext)
                    .load(item.getImage())
                    .error(R.mipmap.ic_default)
                    .into(productHolder.productImg);
        }
        productHolder.productTitle.setText(item.getProductName());
        productHolder.saleNum.setText("销量" + item.getSaleCount());
        productHolder.priceOld.setText(String.format("市价¥%s", item.getCostPrice().setScale(2, RoundingMode.HALF_UP)));
        productHolder.priceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        productHolder.sellOutImg.setVisibility(item.getStore()==0?View.VISIBLE:View.GONE);
        switch (item.getType()) {
            case 1:
                //普通商品
                productHolder.flagImg.setVisibility(View.GONE);
                productHolder.priceNow.setText(String.format("会员价:¥%s", item.getVipPrice().setScale(2, RoundingMode.HALF_UP)));
                break;
            case 2:
                //秒杀产品
                productHolder.flagImg.setVisibility(View.VISIBLE);
                productHolder.flagImg.setImageResource(R.mipmap.miaosha);
                productHolder.priceNow.setText(String.format("秒杀价:¥%s", item.getPrice().setScale(2, RoundingMode.HALF_UP)));
                break;
            case 3:
                //限时购商品
                productHolder.flagImg.setVisibility(View.VISIBLE);
                productHolder.flagImg.setImageResource(R.mipmap.qianggou);
                productHolder.priceNow.setText(String.format("抢购价:¥%s", item.getPrice().setScale(2, RoundingMode.HALF_UP)));
                break;
        }

        productHolder.totalLayout.setTag(item);

    }


    class ProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.product_img)
        ImageView productImg;
        @Bind(R.id.flag_img)
        ImageView flagImg;
        @Bind(R.id.product_title)
        TextView productTitle;
        @Bind(R.id.sale_num)
        TextView saleNum;
        @Bind(R.id.price_old)
        TextView priceOld;
        @Bind(R.id.price_now)
        TextView priceNow;
        @Bind(R.id.total_layout)
        RelativeLayout totalLayout;
        @Bind(R.id.sell_out_img)
        ImageView sellOutImg;

        public ProductHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            totalLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Product item = (Product) v.getTag();
            Intent intent = new Intent(mContext, ProductDetailActivity.class);
            intent.putExtra("type", item.getType());
            intent.putExtra("productId", item.getProductId());
            mContext.startActivity(intent);
        }
    }


    @Override
    public int getItemCount() {
        return dataSet.size();
    }
}
